package com.aipartner.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 分页结果封装类
 * 
 * @author AI伙伴团队
 * @since 2024-01-20
 * @param <T> 数据类型
 */
@Data
@EqualsAndHashCode(callSuper = true)
@ApiModel(description = "分页结果")
public class PageResult<T> extends Result<List<T>> {
    
    @ApiModelProperty(value = "总记录数", example = "100")
    private Long total;
    
    @ApiModelProperty(value = "总页数", example = "10")
    private Long pages;
    
    @ApiModelProperty(value = "是否有上一页", example = "false")
    private Boolean hasPrevious;
    
    @ApiModelProperty(value = "是否有下一页", example = "true")
    private Boolean hasNext;
    
    @ApiModelProperty(value = "当前页码", example = "1")
    private Integer current;
    
    @ApiModelProperty(value = "每页大小", example = "10")
    private Integer size;
    
    public PageResult() {
        super();
    }
    
    public PageResult(Integer code, String message, List<T> data, Long total, Long pages, 
                     Boolean hasPrevious, Boolean hasNext, Integer current, Integer size) {
        super(code, message, data, true);
        this.total = total;
        this.pages = pages;
        this.hasPrevious = hasPrevious;
        this.hasNext = hasNext;
        this.current = current;
        this.size = size;
    }
    
    /**
     * 成功返回分页数据
     */
    public static <T> PageResult<T> success(List<T> data, Long total, Long pages, Boolean hasPrevious, Boolean hasNext) {
        PageResult<T> result = new PageResult<>();
        result.setCode(ResultCode.SUCCESS.getCode());
        result.setMessage(ResultCode.SUCCESS.getMessage());
        result.setData(data);
        result.setTotal(total);
        result.setPages(pages);
        result.setHasPrevious(hasPrevious);
        result.setHasNext(hasNext);
        result.setSuccess(true);
        return result;
    }
    
    /**
     * 构建分页结果
     */
    public static <T> PageResult<T> build(List<T> data, Long total, Long pages, Boolean hasPrevious, Boolean hasNext, Integer code, String message) {
        PageResult<T> result = new PageResult<>();
        result.setCode(code);
        result.setMessage(message);
        result.setData(data);
        result.setTotal(total);
        result.setPages(pages);
        result.setHasPrevious(hasPrevious);
        result.setHasNext(hasNext);
        result.setSuccess(ResultCode.SUCCESS.getCode().equals(code));
        return result;
    }
}